package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author 李红磊
 * @since 2021-06-13
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;

    @Override//chapter页的章节和小节显示
    public List<ChapterVo> getChapterVideoService(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> eduChapters = this.list(wrapper);//获取该课程的章节列表

        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("course_id",courseId);

        List<EduVideo> videos = eduVideoService.list(wrapper2);//小节列表
        ArrayList<ChapterVo> chapterVoList = new ArrayList<>();//最终返回的列表


        for (int i = 0; i < eduChapters.size(); i++) {
            EduChapter chapter = eduChapters.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);


            ArrayList<VideoVo> videoVoList = new ArrayList<>();//ChapterVo的属性


            for (int m = 0; m < videos.size(); m++) {
                EduVideo video = videos.get(m);

                if(video.getChapterId().equals(chapterVo.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoVoList.add(videoVo);
                }

            }

            chapterVo.setChildren(videoVoList);
            chapterVoList.add(chapterVo);


        }
        

        return chapterVoList;
    }

    @Override//删除章节。如果章节下面有小节，就不让删除
    public int deleteChapter(String id) {
        //根据章节id查询小节表，若小节表中有数据。则不让删除。
        QueryWrapper<EduVideo> eduVideoQueryWrapper = new QueryWrapper<>();
        eduVideoQueryWrapper.eq("chapter_id",id);
        int count = eduVideoService.count(eduVideoQueryWrapper);
        if(count==0){
            boolean b = this.removeById(id);
            if(b){
                return 1;
            }else {
                return 0;

            }

        }else {
            return 0;
        }



    }

    @Override
    public void removeChapterByCourseById(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
